package com.sxt.core.util;

import java.util.HashMap;
import java.util.Map;

public class ThreadLocalUtil {
	/**
	 * 线程里面设置值
	 */
	private static ThreadLocal<Map<String,Object>> threadValue = new ThreadLocal<Map<String,Object>>(){

		protected java.util.Map<String,Object> initialValue() {

			return new HashMap<String, Object>();

		};
	};


	/**
	 * 给线程里面设置值 
	 * @param name
	 *  值的名称
	 * @param object
	 * 值
	 */
	public  static void  setValue(String name,Object object) {
		Map<String, Object> threadLocalValue = threadValue.get();
		threadLocalValue.put(name, object);
	}

	/**
	 * 从线程里面获取值
	 * @param name
	 * @return
	 */
	public static Object getValue(String name) {
		Map<String, Object> threadLocalValue = threadValue.get();
		return threadLocalValue.get(name);
	}

	/**
	 * 清空线程里面的值
	 */
	public static void clearValue() {
		Map<String, Object> threadLocalValue = threadValue.get();
		threadLocalValue.clear();
	}

	public static void main(String[] args) {
		setValue("main", "mail");
		Object value = getValue("main");
		System.out.println(value);
		
		
		new Thread(new Runnable() {

			@Override
			public void run() {
				System.out.println(Thread.currentThread().getName()+":取main：：：："+getValue("main"));
				setValue("sub-thread", "sub");
				System.out.println(Thread.currentThread().getName()+":取sub-thread：：：："+getValue("sub-thread"));
			}
		}).start();
		
		
		
		Object value2 = getValue("sub-thread");
		System.out.println(value2);
	}
}
